#include "Camera.h"
#include "D3DApp.h"
#include "D3DInput.h"

Camera* gCamera = 0;

Camera::Camera(){

	D3DXMatrixIdentity(&mOrtho2D);
	D3DXMatrixIdentity(&mIdentity);
	mPosW		= D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mOldX		= 0;
	mOldY		= 0;
	mOldWidth	= 0;
	mOldHeight	= 0;
}

void Camera::setMapSize(RECT size){
	this->mMapSize = size;
}

void Camera::setView(){
	HR(gDevice->SetTransform(D3DTS_WORLD, &mIdentity));
	HR(gDevice->SetTransform(D3DTS_VIEW, &mIdentity));
}

void Camera::setPos(int x, int y, bool needy){
	int left = x - gApp->mWidth/2;
	int right = x + gApp->mWidth/2;

	if(left>=mMapSize.left && right<=mMapSize.right){
		mPosW.x = x;
	}else{
		if(left<mMapSize.left){
			mPosW.x = mMapSize.left + gApp->mWidth/2;
		}else{
			mPosW.x = mMapSize.right - gApp->mWidth/2;
		}
	}

	if(needy){
		if(mPosW.y == 0){
			mPosW.y = y + gApp->mHeight/2 -24;
			mOldY = y;
		}else{
			if(y>mOldY){
			}else{
				mPosW.y = y + gApp->mHeight/2-24;
				mOldY = y;
			}
		}
	}
	
}

void Camera::reset(int x, int y){
	mPosW.y = y + gApp->mHeight/2 -24;
	mOldY = y;
}

void Camera::setLens(float width, float height, float nearZ, float farZ){
	
	D3DXMatrixOrthoLH(&mOrtho2D,width,height,nearZ, farZ);
	HR(gDevice->SetTransform(D3DTS_PROJECTION,&mOrtho2D));
}

D3DXVECTOR3& Camera::getPos(){
	return mPosW;
}

RECT Camera::getAABB(){
	return mAABB;
}

void Camera::update(float dt){
	mAABB.left = mPosW.x - gApp->mWidth/2;
	mAABB.right = mPosW.x + gApp->mWidth/2;
	mAABB.bottom = mPosW.y - gApp->mHeight/2;
	mAABB.top = mPosW.y + gApp->mHeight/2;
}